package at.main.gui;

import at.main.android.Graphics;
import at.main.util.ManagerFont;
import at.main.util.MyRecordStore;

/*
 * Quy dinh trong 
 **/
public class ExitGamePage {
	public int iSelectMenu = 0;

	public MyRecordStore mrs;

	public ExitGamePage() {
		iSelectMenu = 1;
	}

	public void paint(Graphics g) {
		g.setClip(0, 0, MyCanvas.iWidth, MyCanvas.iHeight);
		g.setColor(0x87CEFF);
		g.fillRect(0, 0, MyCanvas.iWidth, MyCanvas.iHeight);
		ManagerFont.drawString(g, "Bạn có muốn thoát game ?",
				MyCanvas.iWidth / 2, MyCanvas.iHeight / 3, 2, 2);
		ManagerFont.drawString(g, "Thoát", MyCanvas.iWidth / 2,
				MyCanvas.iHeight / 3 + 15, 2, ((0 - iSelectMenu) == 0 ? 1 : 2));
		ManagerFont.drawString(g, "Không", MyCanvas.iWidth / 2,
				MyCanvas.iHeight / 3 + 30, 2, ((1 - iSelectMenu) == 0 ? 1 : 2));
	}

	/*
	 * Quy định lưu của myReadrecord 
	 * + PacMan: vị trí , hướng,số mạn còn lại, level , điểm, vận tốc, iCountPickup,Items+ iCountMonsterEta +
	 * ManagerMonster : số con monster, trạng thai nào, vận tốc 
	 * - Monster: vi tri, hướng,song hay chet 
	 * + lưu thông tin của map
	 */
	public boolean WriteRS() {
		if (mrs == null)
			mrs = new MyRecordStore();
		mrs.openRecStore();
		String str = "";
		
		// PacMan
		str += PacMan.iX + "|" + PacMan.iY + "|" + PacMan.iStatus+"|"+PacMan.iCountlife + "|"
				+ PacMan.iLevel + "|" + PacMan.iScore + "|" + PacMan.iV + "|"
				+ PacMan.iCountPickup + "|" + PacMan.iCountItemsEta + "|"
				+ PacMan.iCountMonsterEta + "|";
		// ManagerMonster
		str += ManagerMonster.iCountMonster + "|";
		if (ManagerMonster.isState) {
			str += "1|";
		} else {
			str += "0|";
		}
		str+=ManagerMonster.iV+"|";
		int i =0;
		int j = 0;
		for(i =0; i< ManagerMonster.iCountMonster;i++){
			str+=ManagerMonster.arrMonster[i].iX+"|"+ManagerMonster.arrMonster[i].iY+"|"+ ManagerMonster.arrMonster[i].iStatus+ "|";
			if(ManagerMonster.arrMonster[i].isLife)
			{
				str+="1";
			}else{
				str+="0";
			}
		}
		//Luu thong cua map
		for(i =0; i < PlayGame.k_row;i++){
			for(j =0; j < PlayGame.k_column;j++){
				str+="|"+ PlayGame.arrMap[i][j];
			}
		}
		
		System.out.println(">>>> "+ str);
//		byte[]b = new byte[str.length()];
//		for(i =0; i < str.length();i++ ){			
//			b[i] = Byte.parseByte(c);
//		}
		mrs.writeRecord(str);
		mrs.closeRecStore();
		return true;
	}

	public void update() {
		// Up
		if (MyCanvas.keyHold[2]) {
			iSelectMenu--;
			if (iSelectMenu < 0)
				iSelectMenu = 1;
		}
		if (MyCanvas.keyHold[8]) {
			iSelectMenu++;
			if (iSelectMenu > 1)
				iSelectMenu = 0;
		}
		if (MyCanvas.keyHold[5]) {
			if (iSelectMenu == 0) {
				if (WriteRS())
					MainMidlet.midlet.destroyApp(true);
			} else if (PlayGame.isPause) {
				MyCanvas.iCurrentPage = MyCanvas.k_MenuPlaying;
			} else {
				MyCanvas.iCurrentPage = MyCanvas.k_MenuPage;
			}
			MyCanvas.clearInput();
		}
		MyCanvas.clearInput();
	}
}
